import sys
from PyQt5.QtWidgets import (QApplication, QDialog, QVBoxLayout, QHBoxLayout,
                             QLabel, QRadioButton, QButtonGroup, QPushButton, QCheckBox)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

class DynamicMultiSelectDialog(QDialog):
    def __init__(self, title, message, options_list, parent=None):
        super().__init__(parent)
        self.options_list = options_list
        self.selected_options = []  # 存储用户选择的多个选项，初始为空列表

        self.setWindowTitle(title)
        self.setModal(True)
        self.resize(400, 300)

        layout = QVBoxLayout(self)

        self.label = QLabel(message)
        self.label.setAlignment(Qt.AlignCenter)
        label_font = QFont("Microsoft YaHei", 18)
        self.label.setFont(label_font)
        layout.addWidget(self.label)

        # 动态创建复选框 (CheckBox)
        option_font = QFont("Microsoft YaHei", 14)
        self.checkboxes = []  # 保存所有复选框对象的引用
        for option_text in options_list:
            checkbox = QCheckBox(option_text)
            checkbox.setFont(option_font)
            self.checkboxes.append(checkbox)
            layout.addWidget(checkbox)

        # 默认选择第一个选项
        if options_list:
            first_button = self.checkboxes[0]
            first_button.setChecked(True)

        button_layout = QHBoxLayout()
        self.ok_button = QPushButton("确定")
        self.ok_button.setFont(QFont("Microsoft YaHei", 13))
        button_layout.addWidget(self.ok_button)
        layout.addLayout(button_layout)

        self.ok_button.clicked.connect(self.accept_selection)

    def accept_selection(self):
        """获取选中的单选按钮的文本"""
        self.selected_options = [cb.text() for cb in self.checkboxes if cb.isChecked()]
        self.accept()  # 关闭对话框并返回 QDialog.Accepted

    def get_selection(self):
        return self.selected_options

# 使用示例
if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 模拟动态变化的选项列表
    dynamic_options = ["选项A", "选项B", "选项C", "选项D"]

    # 创建并显示对话框
    dialog = DynamicMultiSelectDialog("标题", "这是一条重要信息，请选择：", dynamic_options)

    if dialog.exec_() == QDialog.Accepted: # 等待对话框关闭
        selected_value = dialog.get_selection()
        print(f"用户选择了: {selected_value}")
        # 在这里使用 selected_value 进行后续操作
    else:
        print("用户取消了选择")

    sys.exit(app.exec_())